package com.geeke.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.geeke.common.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.data.Page;
import com.geeke.common.data.SearchParams;
import com.geeke.sys.entity.Resource;
import com.geeke.sys.service.ResourceService;
import com.geeke.utils.ResultUtil;

/**
 * 资源表 Controller
 */
@RestController
@RequestMapping(value = "/sys/resource")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        Resource entity = resourceService.get(id);
        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        Page<Resource> result = resourceService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }
    
    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<Resource> result = resourceService.listAll(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "save")
    @RequiresPermissions(value={"resource:create","resource:update"}, logical= Logical.OR)
    public ResponseEntity<JSONObject> save(@RequestBody Resource resource) {
        String id = resourceService.save(resource).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    @PostMapping(value = "delete")
    @RequiresPermissions("resource:delete")
    public ResponseEntity<JSONObject> delete(@RequestBody Resource resource) {
        int rows = resourceService.delete(resource);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    @PostMapping(value = "bulkInsert")
    @RequiresPermissions("resource:create")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody List<Resource> resources) {
        List<String> ids = resourceService.bulkInsert(resources);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkUpdate")
    @RequiresPermissions("resource:update")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody List<Resource> resources) {
        List<String> ids = resourceService.bulkUpdate(resources);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkDelete")
    @RequiresPermissions("resource:delete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody List<Resource> resources) {
        int rows = resourceService.bulkDelete(resources);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }
} 